package com.airtight.objects;


import com.airtight.game.VertexArray;
import com.airtight.programs.TextureShaderProgram;

import static android.opengl.GLES20.GL_TRIANGLE_FAN;
import static android.opengl.GLES20.glDrawArrays;
import static com.airtight.game.Constants.BYTES_PER_FLOAT;

public class Object {
    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int TEXTURE_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT + TEXTURE_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    private final VertexArray vertexArray;

    Object(float x, float y, float width, float height) {

        float[] VERTEX_DATA = {
                // Order of coordinates: X, Y, S, T

                // Triangle Fan
                x, y, 0.5f, 0.5f,
                x - width, (y - height), 0f, 1f,
                x + width, (y - height), 1f, 1f,
                x + width, (y + height), 1f, 0f,
                x - width, (y + height), 0f, 0f,
                x - width, (y - height), 0f, 1f
        };

        vertexArray = new VertexArray(VERTEX_DATA);
    }

    void bindData(TextureShaderProgram textureProgram) {
        vertexArray.setVertexAttribPointer(0, textureProgram.getPosition(), POSITION_COMPONENT_COUNT, STRIDE);
        vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT, textureProgram.getTextureLocation(), TEXTURE_COMPONENT_COUNT, STRIDE);
    }

    public void draw() {
        glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
    }
}
